package com.which.one.deepseek;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.which.one.R;

import java.util.List;

// MessageAdapter.java - RecyclerView适配器
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
    private List<Message> messages;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView messageText;

        public ViewHolder(View view) {
            super(view);
            messageText = view.findViewById(R.id.message_text);
        }
    }

    public MessageAdapter(List<Message> messages) {
        this.messages = messages;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.message_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Message message = messages.get(position);

        holder.messageText.setText(message.getContent());


        // 设置背景和布局方向
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) holder.messageText.getLayoutParams();
        if (message.isUser()) {
            params.horizontalBias = 1.0f; // 右侧对齐
            holder.messageText.setBackgroundResource(R.drawable.user_bubble);
        } else {
            params.horizontalBias = 0.0f; // 左侧对齐
            holder.messageText.setBackgroundResource(R.drawable.ai_bubble);
        }
        holder.messageText.setLayoutParams(params);
    }

    @Override
    public int getItemCount() { return messages.size(); }
}